package cn.rokhdelar.ams.business.biz.project.controller;

import cn.hutool.core.lang.Assert;
import cn.rokhdelar.ams.base.CustomResponse;
import cn.rokhdelar.ams.business.biz.project.domain.po.BizProjectInfo;
import cn.rokhdelar.ams.business.biz.project.service.BizProjectInfoService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
/**
 * @author 范书铭
 * @date 2025年07月14日 14:21
 */
@RestController
@RequestMapping("/bizProjectInfo")
@Tag(name = "项目信息管理", description = "项目信息的增删改查接口")
public class BizProjectInfoController {

    @Autowired
    private BizProjectInfoService bizProjectInfoService;

    /**
     * 新增项目信息
     */
    @PostMapping
    @Operation(summary = "新增项目信息", description = "添加新的项目基本信息")
    public CustomResponse<BizProjectInfo> addProjectByCompany(
            @Parameter(description = "项目信息对象", required = true) 
            @RequestBody BizProjectInfo bizProjectInfo) {
        Assert.isNull(bizProjectInfo.getId());
        if(bizProjectInfo.getNowStatus()==null){
            bizProjectInfo.setNowStatus(0);
        }
        Assert.isTrue(bizProjectInfo.getNowStatus()==0||bizProjectInfo.getNowStatus()==1,"nowStatus只能是0或者1");
        return CustomResponse.success(bizProjectInfoService.addProjectByCompany(bizProjectInfo));
    }

    /**
     * 更新项目信息
     */
    @PutMapping
    @Operation(summary = "更新项目信息", description = "根据ID更新项目基本信息")
    public boolean update(
            @Parameter(description = "项目信息对象（需包含ID）", required = true) 
            @RequestBody BizProjectInfo bizProjectInfo) {
        //只允许某些状态修改
        bizProjectInfo.setUpdateTime(LocalDateTime.now());
        return bizProjectInfoService.updateById(bizProjectInfo);
    }

    /**
     * 根据ID删除项目信息
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除项目信息", description = "根据ID删除指定项目信息")
    public boolean delete(
            @Parameter(description = "项目ID", required = true, example = "1") 
            @PathVariable Long id) {
        return bizProjectInfoService.removeById(id);
    }

    /**
     * 根据ID查询项目信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "查询项目信息", description = "根据ID查询指定项目的详细信息")
    public BizProjectInfo getById(
            @Parameter(description = "项目ID", required = true, example = "1") 
            @PathVariable Long id) {
        return bizProjectInfoService.getById(id);
    }

    /**
     * 查询所有项目信息
     */
    @GetMapping
    @Operation(summary = "查询所有项目", description = "获取所有项目的基本信息列表")
    public List<BizProjectInfo> getAll() {
        return bizProjectInfoService.list();
    }

    /**
     * 分页查询项目信息
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询项目", description = "分页查询项目信息，支持按名称和分类筛选")
    public Page<BizProjectInfo> getPage(
            @Parameter(description = "页码", required = true, example = "1") 
            @RequestParam Integer pageNum,
            @Parameter(description = "每页条数", required = true, example = "10") 
            @RequestParam Integer pageSize,
            @Parameter(description = "项目名称（模糊查询）", example = "道路改造") 
            @RequestParam(required = false) String projectName,
            @Parameter(description = "项目分类（精确查询）", example = "基础设施") 
            @RequestParam(required = false) String projectCategory) {
        
        return bizProjectInfoService.getPage(pageNum, pageSize, projectName, projectCategory);
    }



}
    